package com.ridu.project.beshid.controller;

import com.ridu.project.beshid.config.GlobalConfiguration;
import com.ridu.project.beshid.entity.UserEntity;
import com.ridu.project.beshid.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Created by admi on 2016/10/28.
 */
@Slf4j
public class BaseController {

    @Autowired
    private GlobalConfiguration globalConfiguration;
    @Autowired
    private UserService userService;

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    public static final String SUCCESS = "{\"success\":true}";
    public static final String FAIURE = "{\"success\":false}";

    private Logger logger = LoggerFactory.getLogger(BaseController.class);

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }

    protected String getContextPath() {
        return request.getContextPath();
    }


    protected UserEntity getUser() throws Exception{
        Subject subject = SecurityUtils.getSubject();
        String username = (String)subject.getPrincipal();
        if(username == null){
            throw new Exception("请重新登录");
        }
        return userService.findByUsername(username);
    }

}
